<?php
/**
 * 文件类(文件、目录)
 *
 * @copyright Copyright (c) 2008 www.eefocus.com
 * @author weiqi<weiqi228@gmail.com>
 * @package com
 * @subpackage eefocus
 * @version 1.0 2007-11-5
 */

class Files{
	public static function mkdirs($dir, $mode = 0777) {
		$stack = array (
			basename($dir
		));
		$path = null;
		while (($d = dirname($dir))) {
			if (!is_dir($d)) {
				$stack[] = basename($d);
				$dir = $d;
			} else {
				$path = $d;
				break;
			}
		}

		if (($path = realpath($path)) === false)
			return false;

		$created = array ();
		for ($n = count($stack) - 1; $n >= 0; $n--) {
			$s = $path . '/' . $stack[$n];
			if (!mkdir($s, $mode)) {
				for ($m = count($created) - 1; $m >= 0; $m--)
					rmdir($created[$m]);
				return false;
			}
			$created[] = $s;
			$path = $s;
		}
		return true;
	}

	 /**
	  * 删除该目录下的所有文件及文件夹
	  *
	  * @param string $dir - 目录 比如c:\www1\ 为绝对路?
	  * @param string $tag - true:同时删除该目录，
	  *						 false:仅仅删除该目录下的文件及子目录
	  * @return bool
	 */
	public static function rmdirs($dir, $tag = false) {
		//定义函数作用区域，避免误删除整个网站
		$parent = DOCUROOT."/data/";

		//本函数仅可删除data目录下的文件;
		$self = substr($dir,0,strlen($parent));	
		if($self != $parent)	return false;
		
		//data后的第一级目录必须存在
		$first=substr($dir,strlen($parent));
		$pos= strpos($first ,"/",0 );
		if($pos==0)	return false;

		//只能对data后的第二级目录执行删除操作
		$second= substr( $first,$pos+1 );
		$checkpos = strpos($second,"/",0 );
		if( $checkpos==0 ) return false;

		if( $handle = @opendir( $dir ) ){
			while( ( $file = @readdir( $handle ) ) !== false ){
				if( $file != "." && $file != ".." ){
					$filepath = $dir."/".$file;
					if( is_file( $filepath ) ){
						@unlink( $filepath );
					}elseif( is_dir( $filepath ) ){
						self::rmdirs( $filepath,true );
					}
				}
			}
			closedir( $handle );
		}
	
		if( $tag ){
			@rmdir( $dir );
		}
		return true;
	}

	/**
	 * 取得字节数所对应的相关单位值
	 * @author weiqi<weiqi@eefocus.com>
	 * @param string $lenght
	 * @return string
	 */
	public static function setupSize($lenght) {
		$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB','EB','ZB','YB');
		foreach ($units as $unit) {
			if($lenght>1024) $lenght = round($lenght/1024, 1);
			else break;
		}
		if(intval($lenght)==0){return("0 Bytes");}
		return $lenght.' '.$unit;
	}

	### Read folder size
	public function folderSize($dir) {
		if (!preg_match('#/$#', $dir)) {
			$dir .= '/';
		}
		$totalsize = 0;
		foreach (self::fileList($dir) as $name) {
			$totalsize += (@ is_dir($dir . $name) ? self::folderSize("$dir$name/") : (int) @ Filesize($dir .
			$name));
		}
		return $totalsize;
	}
	### Get all Files
	public function fileAll($path) {
		$list = array ();
		if (($hndl = @ opendir($path)) === false) {
			return $list;
		}
		while (($file = readdir($hndl)) !== false) {
			if ($file != '.' && $file != '..') {
				$list[] = $file;
			}
		}
		closedir($hndl);
		return $list;
	}
	### Get Files list
	public function fileList($path) {
		if (!preg_match('#/$#', $path)) {
			$path .= '/';
		}
		$f = $d = array ();
		foreach (self::fileAll($path) as $name) {
			if (@ is_dir($path . $name)) {
				$d[] = $name;
			} else
				if (@ is_file($path . $name)) {
					$f[] = $name;
				}
		}
		natcasesort($d);
		natcasesort($f);
		return array_merge($d, $f);
	}
	### get file info
	public static function getExt($filename) {
		if (strstr($filename, "\\") || strstr($filename, "/")) {
			$filename = basename($filename);
		}
		if (strstr($filename, ".")) {
			return mb_substr(strrchr($filename, '.'), 1);
		} else {
			return false;
		}
	}
	public static function getFolder($dir){
		$folder=array();
		if ($handle = opendir($dir)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != ".." &&substr($file,0,1)!=".") {
					if(is_dir($dir.$file)){
						$folder[]= $file;
					}			            
				}
			}
			closedir($handle);
		}
		return $folder;
	}
	public static function getFile($dir){
		$Files=array();
		if ($handle = opendir($dir)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != ".." &&substr($file,0,1)!=".") {
					if(is_file($dir.$file)){
						$Files[]= $file;
					}			            
				}
			}
			closedir($handle);
		}
		return $Files;
	}
	/**
	 * Make Files with some text character
	 * @param string $file_name
	 * @param string $content
	 * @return bool
	 */
	public static function makeFile($file_name, $content) {
		if (!file_exists($file_name)) {
			$fp = fopen($file_name, "w+");
		} else {
			$fp = fopen($file_name, "w");
		}
		if (!$fp) {
			return false;
		}

		if (!fwrite($fp, $content)) {
			fclose($fp);
			return false;
		} else {
			fclose($fp);
			return true;
		}
	}
}
?>
